// pages/function/warm-up1.js
var util = require('../../utils/util');
var that;
var inter;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barimages: ['../../images/bnav1h.png', '../../images/bnav2n.png', '../../images/bnav3n.png', '../../images/bnav4n.png'],
    pro: '0%',
    recordurl: '../../images/ic_record.png',
    playbtn: '../../images/ic-play.png',
    scrollHeight:'667',
    videoList:[],
    hidden: true,
    videoSrc:'',
    videoWidth: '100%',
    videoHeight: '300',
    nowStep:1,
    showEnd:false,
    leftTime:300,
    showTime:'05:00',
    scrollTop:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
 
    wx.getSystemInfo({
      success: function(res) {
        that.setData({ scrollHeight: res.windowHeight - 164, videoHeight: res.windowWidth });
      }
    })
    
    util.sendRequest('WxGetVideoExList', { week: wx.getStorageSync('tempWeek'), step: wx.getStorageSync('tempStep') }, function (resp) {
      for(var i = 0 ; i < resp.result.length ; i ++){
        resp.result[i].costMinute = Math.floor(resp.result[i].time / 60);
      }
      that.setData({ videoList: resp.result });
      if (resp.result.length > 0) that.startInterval(0);
    }, '加载中...', function () {

    });
  },
  second2Minute:function(second){
    var minute = Math.floor(second / 60);
    var second = second %60;
    return that.formatZero(minute) + ":" + that.formatZero(second);
  },
  formatZero:function(num){
    if(num<10) return "0"+num;
    return num;
  },
  startInterval:function(index){
    that.setData({ nowStep:(index+1)});
      var item = that.data.videoList[index];

      if(item==undefined){
        var h1 = 281;
        var h2 = 230;
        var scrollTop = 0;
        for (var i = 0; i < that.data.videoList.length; i++) {
          if (that.data.videoList[i].type == 1) {
            scrollTop += h1;
          } else {
            scrollTop += h2;
          }
        }
        that.setData({ showEnd: true, scrollTop: scrollTop+100 })
        return;
      }
      var totalTime = item.time;
      that.setData({ showTime: that.second2Minute(totalTime) });
      inter = setInterval(function(){
        if (totalTime==0){
          clearInterval(inter);
          var nowIndex = index + 1;
          var scrollTop = 0;
          var h1 = 281;
          var h2 = 230;
          for (var i = 0; i < nowIndex ; i++){
            if (that.data.videoList[i].type==1){
              scrollTop+=h1;
            }else{
              scrollTop+=h2;
            }
          }
          that.setData({ scrollTop: scrollTop })
          that.startInterval(nowIndex);
          return;
        }
        totalTime = totalTime-1;
        that.setData({ showTime: that.second2Minute(totalTime) });
      }, 1000);
  },
  hideVideo: function () {
    that.setData({ hidden: true });
    var videoContext = wx.createVideoContext('myVideo');
    videoContext.pause();
  },
  playVideo: function (e) {
    that.setData({ videoSrc:e.currentTarget.dataset.fileid,hidden:false})
    var videoContext = wx.createVideoContext('myVideo');
    videoContext.play();
  },
  overDl:function(){
    util.sendRequest('WxOverExec', { week: wx.getStorageSync('tempWeek'), step: wx.getStorageSync('tempStep') },    function(resp){

      if (resp.code==0){
        wx.setStorageSync('intent', resp.text);
        wx.reLaunch({
          url: '/pages/function/completed',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        });
      }
    },'提交中...',function(){

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})